class Lrc {
  constructor(lrcContent, updateLineCallback) {
    this.lines = this.parseLines(lrcContent);
    this.updateLineCallback = updateLineCallback;
    this.lastLine = -1;
  }

  parseLines(lrcContent) {
    const lines = lrcContent.split('\n');
    for(let i = 0; i < lines.length; i++) {
      lines[i] = this.parseLine(lines[i]);
    }
    return lines.filter(line => line.text && line.time >= 0);  // 清除空行和错误行
  }

  parseLine(line) {
    const match = /\[(\d{2}):(\d{2}\.\d{2})](.*)/.exec(line);
    if(!match) return {};
    const minutes = parseInt(match[1]);
    const seconds = parseFloat(match[2]);
    const time = minutes * 60 + seconds;
    const text = match[3].trim();
    return { time, text };
  }

  seek(time) {
    for(var i = 0; i < this.lines.length; i++) {
      if(this.lines[i].time > time) break;
    }
    const line = i - 1;
    if(line !== this.lastLine) {
      this.lastLine = line;
      this.updateLineCallback(this.lines[line].text);
    }
  }
}